package com.zktr.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import com.zktr.bean.CategoryBean;
import com.zktr.bean.GoodTypeBean;
import com.zktr.bean.GoodsBean;
import com.zktr.dao.GoodTypeDao;
import com.zktr.pojo.Category;
import com.zktr.pojo.Goods;
import com.zktr.pojo.GoodsType;
import com.zktr.pojo.PageBean;

public class GoodTypeService {
	GoodTypeDao gTDao = new GoodTypeDao();
	public static void main(String[] args) {
		GoodTypeService goodsService = new GoodTypeService();
		PageBean<GoodTypeBean> findAllGoodsType = goodsService.findAllGoodsType(1, 5);
		List<GoodTypeBean> beanList = findAllGoodsType.getBeanList();
		for (GoodTypeBean goodTypeBean : beanList) {
			System.out.println(goodTypeBean);
		}
		System.out.println(beanList);
		
		System.out.println(findAllGoodsType);
		
		}
	/**
	 * 查询所有信息，并分页，以及数据类型装换
	 * @param pc
	 * @param ps
	 * @return
	 */
	public PageBean<GoodTypeBean> findAllGoodsType(int pc, int ps) {
	
		List<GoodsType> selectGoods = gTDao.selectGoods(pc, ps);
		List<GoodTypeBean> list2 = new LinkedList<>();
		findAllGoodsType(selectGoods,list2);
		PageBean<GoodTypeBean> bean = new PageBean<GoodTypeBean>();
		bean.setPc(pc);
		bean.setPs(ps);
		bean.setBeanList(list2);
		bean.setTr(gTDao.selectGoodsCount());
		return bean;
	}

	/**
	 * 时间类型转换
	 * @param addtime
	 * @return
	 */
	private static String mydate(int addtime) {
		Calendar cal = Calendar.getInstance();
		long millions = new Long(addtime).longValue() * 1000;
		cal.setTimeInMillis(millions);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(cal.getTime());
	}
	  private static void findAllGoodsType(List<GoodsType> list, List<GoodTypeBean> list2) {
			for (GoodsType goods : list) {
				GoodTypeBean cBeans = new GoodTypeBean();
				cBeans.setTypeid(goods.getTypeid()+"");
				cBeans.setTypename(goods.getTypename());
				if (goods.getEnabled()) {
					cBeans.setEnabled("../img/yes.gif");
				}else {
					cBeans.setEnabled("../img/err.gif");
				}
				cBeans.setAttrgroup(goods.getAttrgroup());
				list2.add(cBeans);
			}
			
	    }
}
